package com.nuonuo.thread;

import com.nuonuo.listener.RedisDelayedQueueListener;
import org.redisson.RedissonShutdownException;
import org.redisson.api.RBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author duan
 * @version 1.0
 * @date 2024/1/25 16:47
 */
public class QueueListenerThread<T> extends Thread {

    private static final Logger LOGGER = LoggerFactory.getLogger(QueueListenerThread.class);

    private final RBlockingQueue<T> blockingFairQueue;
    private final RedisDelayedQueueListener<T> redisDelayedQueueListener;

    public QueueListenerThread(RBlockingQueue<T> blockingFairQueue, String name,
                               RedisDelayedQueueListener<T> redisDelayedQueueListener) {
        this.blockingFairQueue = blockingFairQueue;
        this.redisDelayedQueueListener = redisDelayedQueueListener;
        super.setName(name);
    }

    @Override
    public void run() {
        while (true) {
            try {
                LOGGER.info("---> prepare to take");
                T t = blockingFairQueue.take();
                redisDelayedQueueListener.invoke(t);
            } catch(Exception exception){
                exception.printStackTrace();
            }
        }
    }
}
